# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=libuv
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.42.0
pkgrel=3
pkgdesc="Cross-platform asynchronous I/O (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/libuv/libuv"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=(https://dist.libuv.org/dist/v${pkgver}/libuv-v${pkgver}.tar.gz{,.sign}
        "string-encoding-uv_os_gethostname.patch::https://github.com/libuv/libuv/commit/95f88f4.patch"
        "0001-winapi-fix-redefinition.patch")
sha256sums=('43129625155a8aed796ebe90b8d4c990a73985ec717de2b2d5d3a23cfe4deb72'
            'SKIP'
            '18e4c2af57254f0550c684d48b259b54f9bcad27b4286aad1d8c4e5ec0932fc5'
            '925567b88d690950af1201153df641cbf0a3161546da33bcc9a2ac215dc3fd6a')
# PGP key IDs are available from https://github.com/libuv/libuv/blob/v1.x/MAINTAINERS.md
validpgpkeys=('AEAD0A4B686767751A0E4AEF34A25FB128246514') # Jameson Nash <vtjnash@gmail.com>"

prepare() {
  cd "${srcdir}/${_realname}-v${pkgver}"

  # Revert the upstream commit to support Windows 7  because
  # GetHostNameW is supported from Windows 8. But this also
  # reintroduces issue in uv_os_gethostname() which returns
  # grabled code if the name contains non ascii characters
  # https://github.com/msys2/MINGW-packages/issues/9632

  patch -R -p1 -i "${srcdir}/string-encoding-uv_os_gethostname.patch"

  patch -p1 -i "${srcdir}/0001-winapi-fix-redefinition.patch"

 ./autogen.sh
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  export lt_cv_deplibs_check_method='pass_all'

  ../${_realname}-v${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST}

  make
}

check(){
  cd "${srcdir}/build-${MINGW_CHOST}"
  # One these tests fails
  make check || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  install -D -m644 "${srcdir}"/${_realname}-v${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
